package com.comtop.db.hive

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.sql.hive.HiveContext

object ConnectHive {
  
  def main(args: Array[String]): Unit = {
		  val conf = new SparkConf().setAppName("SparkSQL Demo").setMaster("local[2]")
				  val sc = new SparkContext(conf)
		  val sqlContext = new HiveContext(sc)
		  
		  sqlContext.sql("CREATE TABLE IF NOT EXISTS t_student (username STRING, age INT) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE")
		  sqlContext.sql("LOAD DATA LOCAL INPATH '/root/user.txt' INTO TABLE t_student")
		  
		  // Queries are expressed in HiveQL
		  sqlContext.sql("FROM t_student SELECT username, age").collect().foreach(println)
  }
  
}